package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.model.Clazz;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.model.RespPageBean;
import com.qf.studentmanager.model.Student;
import com.qf.studentmanager.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Student")
public class StudentServlet extends HttpServlet {
    StudentService studentService = new StudentService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;
//        Student student = objectMapper.readValue(request.getInputStream(), Student.class);
        String number = request.getParameter("number");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String qq = request.getParameter("qq");
        String gid = request.getParameter("gid");
        String cid = request.getParameter("cid");

        Student student = new Student(number,name,gender,phone,qq,Integer.parseInt(cid),Integer.parseInt(gid));
        Integer i =studentService.addStudent(student);
        if (i == 1){
            respBean= RespBean.ok("添加成功");
        }else if (i == -2){
            respBean= RespBean.ok("添加失败，学生重复添加");
        }else if (i == -1){
            respBean= RespBean.ok("添加失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String action = request.getParameter("action");

        if ("page".equals(action)) {
            request.getRequestDispatcher("WEB-INF/jsp/student/studentList.jsp").forward(request, response);
        } else if ("data".equals(action)) {
            String page = request.getParameter("page");
            String rows = request.getParameter("rows");
            String sort = request.getParameter("sort");
            String order = request.getParameter("order");
            String gid = request.getParameter("gid");
            String cid = request.getParameter("cid");
            //获取全部学生数据getallstudent(){}返回resppagebean
            RespPageBean respPageBean = studentService.getAllStudentBypage(page, rows, sort, order, gid, cid);
            String string = objectMapper.writeValueAsString(respPageBean);
            response.getWriter().write(string);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;
        String[] sids = request.getParameterValues("sid");

        Integer i =studentService.deleteStudents(sids);
        if (i == 1){
            respBean= RespBean.ok("删除成功");
        }else if (i == -1){
            respBean= RespBean.ok("删除失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;
//        String number = request.getParameter("number");
//        String name = request.getParameter("name");
//        String gender = request.getParameter("gender");
//        String phone = request.getParameter("phone");
//        String qq = request.getParameter("qq");
//        String gid = request.getParameter("gid");
//        String cid = request.getParameter("cid");
//        Student student = new Student(number,name,gender,phone,qq,Integer.parseInt(cid),Integer.parseInt(gid));

        Student student = objectMapper.readValue(request.getInputStream(), Student.class);

        Integer i =studentService.updateStudents(student);
        if (i == 1){
            respBean= RespBean.ok("修改成功");
        }else if (i == -1){
            respBean= RespBean.ok("修改失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }
}
